博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSM-CRUD(2)---查询
阅读量:4086 次
发布时间:2019-05-25

本文共 7734 字,大约阅读时间需要 25 分钟。

逻辑分析:启动项目后,进入到index.jsp页面,点击index.jsp页面的查询员工链接,发出查询员工列表请求,来到empList.jsp页面。

empList.jsp页面使用插件pageHelper完成分页功能。使用pageHelper需要给它几个参数:我要查询第几页;每页查询几条记录;需要连续显示几条记录?

第1步、index.jsp页面如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    		
员工查询
部门查询

第2步、在src/main/java下新建包com.cn.controller,然后在该包下新建类EmpController,代码如下:

package com.cn.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;/** * 处理员工CRUD请求的控制层 * */@Controllerpublic class EmpController {	@RequestMapping("/empList")	public String toEmpList(){		return "empList";	}}

第3步、在WEB-INF下新建文件夹jsp,然后在jsp下新建empList.jsp页面,页面内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>      <%    	pageContext.setAttribute("path",request.getContextPath());    %>    员工列表	
SSM-CRUD
序号 员工名称 性别 邮箱 所在部门 操作
1 张三 zhangsan@163.com 研发部
当前记录数:xxx

注:逻辑是点击index.jsp页面的员工查询,<a href="empList">会根据empList去Controller中查找名为empList的映射,找到后会执行EmpController中的toEmpList()方法,然后返回empList页面,去/WEB-INF/jsp/empList.jsp返回响应。

执行结果如下:

*************************************************************************************************************************************************上面是固定的写法,实际应用中需要去后台查询数据,将后台的数据分页显示到前端页面中

第4步、编写EmpController实现分页,代码如下:

编写前,需要在pom.xml中引入分页插件PageHelper的相关依赖,如下:

com.github.pagehelper
pagehelper
5.0.0
com.github.miemiedev
mybatis-paginator
1.2.17
com.github.jsqlparser
jsqlparser
0.9.5

EmpController分页代码如下:

package com.cn.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import com.cn.bean.Employee;import com.cn.service.EmpService;import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;/** * 处理员工CRUD请求的控制层 * */@Controllerpublic class EmpController {	@Autowired	private EmpService empService;	/**	 * 跳转员工页面	 * */	@RequestMapping("/empList")	public String toEmpList(){		return "empList";	}		/**	 * 分页查询员工数据	 * */	@RequestMapping("/emps")	public String getEmpByPage(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){		//在查询之前传入分页参数:起始页和每页记录数		PageHelper.startPage(pn, 5);		//分页查询,需要使用empService		List
empList=empService.getAll(); //使用pageInfo对结果进行包装,只需要将pageInfo交给页面处理即可(pageInfo里面封装了分页的详细信息 @SuppressWarnings({ "rawtypes", "unchecked" }) PageInfo pageInfo=new PageInfo(empList,5); model.addAttribute("pageInfo", pageInfo); //跳转到查询页面 return "empList"; }}

EmpService代码如下:

package com.cn.service;import java.util.List;import com.cn.bean.Employee;/** * 员工service * */public interface EmpService {	//查询所有员工	public List
getAll();}

EmpService实现类EmpServiceImpl代码如下:

package com.cn.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.cn.bean.Employee;import com.cn.dao.EmployeeMapper;import com.cn.service.EmpService;@Service("empService")public class EmpServiceImpl implements EmpService {		@Autowired	private EmployeeMapper empMapper;		//没有查询条件的,查询所有(带部门信息)	@Override	public List
getAll() { return empMapper.selectByExampleWithDept(null); }}

使用mock模拟请求,测试emps是否可以拿到返回值

package com.cn.test;import java.util.List;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MvcResult;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;import com.cn.bean.Employee;import com.github.pagehelper.PageInfo;/** * 使用spring的单元测试来进行模拟发送请求的测试 * */@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:spring-mvc.xml"})public class MVCTest {	//springMvc的IOC容器	@Autowired	private WebApplicationContext context;	//模拟mvc请求	private MockMvc mockMvc;	@Before	public void setup(){		mockMvc=MockMvcBuilders.webAppContextSetup(context).build();	}	@Test	public void testPage() throws Exception{		//模拟请求拿到返回值		MvcResult result=mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "2")).andReturn();		//请求域中有pageInfo		PageInfo page=(PageInfo) result.getRequest().getAttribute("pageInfo");		System.out.println(page.getPageNum());		System.out.println(page.getTotal());				//得到员工数据		List
empList=page.getList(); for(Employee employee:empList){ System.out.println("ID:"+employee.getEmpId()+"员工名称:"+employee.getEmpName()); } }}

证明pageInfo中拿到值,那么再使用bootstrap的删格系统快速搭建简单的界面,页面empList.jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>      <%    	pageContext.setAttribute("path",request.getContextPath());    %>    员工列表	
SSM-CRUD
序号 员工名称 性别 邮箱 所在部门 操作
${emp.empId} ${emp.empName} ${emp.gender=="M"?"男":"女"} ${emp.email} ${emp.department.deptName}
当前第:${pageInfo.pageNum}页,总共:${pageInfo.pages}页,总共:${pageInfo.total}条记录

注意:一定要在mybatis的配置文件mybatis-conf.xml中配置分页插件

运行项目后,页面效果如下

*************************************************************************************************************************************************至此,页面的查询功能基本完成,分页查询也已实现

转载地址:http://hluii.baihongyu.com/

你可能感兴趣的文章
Git操作清单
查看>>
基础算法
查看>>
前端面试
查看>>
React Hooks 异步操作踩坑记
查看>>
聊聊编码那些事,顺带实现base64
查看>>
TypeScript for React (Native) 进阶
查看>>
React 和 ReactNative 的渲染机制/ ReactNative 与原生之间的通信 / 如何自定义封装原生组件/RN中的多线程
查看>>
JavaScript实现DOM树的深度优先遍历和广度优先遍历
查看>>
webpack4 中的 React 全家桶配置指南,实战!
查看>>
react 设置代理(proxy) 实现跨域请求
查看>>
通过试题理解JavaScript
查看>>
webpack的面试题总结
查看>>
实践这一次,彻底搞懂浏览器缓存机制
查看>>
Koa2教程(常用中间件篇)
查看>>
React Hooks 完全指南
查看>>
React16常用api解析以及原理剖析
查看>>
教你发布你npm包
查看>>
nvm 和 nrm 的安装与使用
查看>>
React Hooks 一步到位
查看>>
React Redux常见问题总结
查看>>